package migrator

import (
	"fmt"
	"gitee.com/xuyiping_admin/pkg/logger/logrus"
	"gorm.io/gorm"
)

// Bootstrap creates "migrations" table
// to keep track of already run database migrations
func Bootstrap(db *gorm.DB) error {
	m := new(Migration)
	exists := db.Migrator().HasTable(m.TableName())
	if exists {
		logrus.Infof("skipping %s migrator", m.TableName())
		return nil
	}
	logrus.Infof("running %s migrator", m.TableName())

	// Create migrations table
	if err := db.AutoMigrate(m); err != nil {
		return fmt.Errorf("error creating migrations table: %s", err)
	}

	return nil
}
